package com.youlai.boot.daily.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.daily.model.entity.DailyRecord;
import com.youlai.boot.daily.service.DailyRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.daily.model.form.DailyRecordForm;
import com.youlai.boot.daily.model.query.DailyRecordQuery;
import com.youlai.boot.daily.model.vo.DailyRecordVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SortedMap;

/**
 * 日常记录前端控制层
 *
 * @author cuisheng
 * @since 2025-04-02 22:51
 */
@Tag(name = "日常记录接口")
@RestController
@RequestMapping("/api/v1/dailyRecords")
@RequiredArgsConstructor
public class DailyRecordController  {

    private final DailyRecordService dailyRecordService;

    @Operation(summary = "日常记录分页列表")
    @GetMapping("/page")
    public Result<SortedMap<LocalDate, List<DailyRecordVO>>> getDailyRecordPage(DailyRecordQuery queryParams ) {
        SortedMap<LocalDate, List<DailyRecordVO>> result = dailyRecordService.getDailyRecordPage(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "日常记录分页列表")
    @GetMapping("/latest")
    public Result<SortedMap<LocalDate, List<DailyRecordVO>>> latest() {
        DailyRecordQuery query = new DailyRecordQuery();
        query.setPageSize(999);
        query.setCreateBy(SecurityUtils.getUserId().toString());

        LocalDate localDate = LocalDate.now();

        // 转换为当天的 00:00:00
        LocalDateTime localDateTime = localDate.atStartOfDay();

        // 转换为秒（UTC 时间）
        Long seconds = localDateTime.toEpochSecond(ZoneOffset.UTC)*1000;
        query.setRecordDate(Collections.singletonList(seconds.toString()));
        return getDailyRecordPage(query);
    }

    @Operation(summary = "新增日常记录")
    @PostMapping
    public Result<Void> saveDailyRecord(@RequestBody @Valid DailyRecordForm formData ) {
        boolean result = dailyRecordService.saveDailyRecord(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取日常记录表单数据")
    @GetMapping("/{id}/form")
    public Result<DailyRecordForm> getDailyRecordForm(
        @Parameter(description = "日常记录ID") @PathVariable Long id
    ) {
        DailyRecordForm formData = dailyRecordService.getDailyRecordFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改日常记录")
    @PutMapping(value = "/{id}")
    public Result<Void> updateDailyRecord(
            @Parameter(description = "日常记录ID") @PathVariable Long id,
            @RequestBody @Validated DailyRecordForm formData
    ) {
        boolean result = dailyRecordService.updateDailyRecord(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除日常记录")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteDailyRecords(
        @Parameter(description = "日常记录ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = dailyRecordService.deleteDailyRecords(ids);
        return Result.judge(result);
    }
}
